// pages/devicedetail/devicedetail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      id: '',
      name: '',
      barcode: '',
      price: '',
      unit: '',
      categoryName: '',
      categoryId: '',
      sort:'',
      diagram: '',
      goodsId: '',
      status:'',
      goodsDiscount: ''
    },
    number: '',
    deviceId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options.deviceId)
    this.setData({
      number: options.number,
      'form.name': options.goodsName,
      'form.price': options.goodsPrice,
      'form.goodsDiscount': options.goodsDiscount,
      'form.sort': options.goodsStock,
      'form.goodsId': options.goodsId,
      'form.id': options.id,
      'form.status': options.status,
      deviceId: options.deviceId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  onSwitchChange(e) {
    console.log('货道开关状态：', e.detail.value);
  },

    //保存
    onSubmit(){
      this.requestHttp('/app/client/mine/goods/selectSave', {'deviceId': this.data.deviceId,'cargoId': this.data.form.id,'goodIds': this.data.form.goodsId,'name': this.data.form.name,'status': this.data.form.status}, 'POST').then((data) => {
        console.log(data)
        if (data.code === 0) {
          wx.showToast({
            title: '保存成功',
          })
          // 返回上一页
        wx.navigateBack({ delta: 1 });
        } else {
          wx.showToast({
            title: data.msg,
          })
        }
      })
    },

    onBindGoodsTap(){
      const that = this
      wx.navigateTo({
        url: '/pages/shopall/shopall',
        events: {
          acceptDataFromOpenedPage(data) {
            console.log('回传数据', data.item)
            that.setData({ 'form.name': data.item.name,'form.id': data.item.id,'form.price': data.item.price,'form.barcode': data.item.barcode,'form.sort': data.item.sort,'form.diagram': data.item.diagram });
          }
        }
      });
    },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  requestHttp(url,data,method){
    return new Promise(function (reslove, reject) {
      let headers = {
        'Authorization': app.globalData.token || '',
        'Content-Type': 'application/x-www-form-urlencoded'
      };
      wx.request({
        url: app.globalData.host + url,
        method: method || 'get',
        data: data,
        header: headers,
        success: function success(res) {
          const data = res.data;
          reslove(data)
          // reslove(res)
        },
        fail: function fail(res) {
          wx.showToast({
            title: res,
            icon: 'error',
          })
          reject(res);
        }
      })
    })
  }
})